package com.aohe.youzi.mvp;

import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.aohe.youzi.MyApplication;
import com.aohe.youzi.utils.EditTextUtil;
import com.aohe.youzi.utils.ToastUtil;


/**
 * Created by ftang on 2018/4/9.
 */
public abstract class UnBaseMvpActivity<P extends MvpPresenter> extends FragmentActivity implements MvpView {

    private P mPresenter;

    @Override
    public void showToast(String message) {
        ToastUtil.showToast(message);
    }

    @Override
    public void showToast(int id) {
        ToastUtil.showToast(id);
    }


    @Override
    public Activity getActivity() {
        return this;
    }


    @Override
    public void showHint(String text) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.destroy();
            mPresenter = null;
        }
    }

    @NonNull
    public abstract P createPresenter();

    /**
     * Subclass can get the bound presenter by calling this method.
     * <p>
     * 子类通过调用该方法，获得绑定的presenter
     *
     * @return Bound presenter
     */
    protected P getPresenter() {
        if (mPresenter == null) {
            mPresenter = createPresenter();
            mPresenter.attachView(this);
        }
        return mPresenter;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //获取当前获得焦点的View
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            try {
                //调用方法判断是否需要隐藏键盘
                EditTextUtil.hideInput(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}
